function [ cst, coloring ] = simAnn3(graph, startingColoring, ...
                                     initialTemperature, coolingRate, ...
                                     maxIterations )
%SIMANN3 Summary of this function goes here
%   Detailed explanation goes here
    it = 0;
    i = 0;
    j = 0;
    age = 2;
    temperature = initialTemperature;
    oldCost = cost(graph, startingColoring);
    coloring = startingColoring;

    while it < maxIterations
        tmpColoring = neighbourColoring(coloring);
        currCost = cost(graph, tmpColoring);
        diff = abs(currCost - oldCost);
        if currCost < oldCost
            coloring = tmpColoring;
            oldCost = currCost;
            it = it + 1;
            i = 0;
            j = j + 1;
        else %currCost >= oldCost
            if rand(1) < exp(-diff/temperature)
                coloring = tmpColoring;
                oldCost = currCost;
                it = it + 1;
                j = j + 1;
            end
            i = i + 1;
            if i >= 100
                it = maxIterations;
            end
        end
        if j > age
            j = 0;
            temperature = coolingRate * temperature;
        end
        %end if currCost < oldCost
    end %end while
cst = cost(graph, coloring);    

end

